package com.gzy.android_douban.ui;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;
import com.gzy.android_douban.R;
import com.gzy.android_douban.application.DialogControl;
import com.gzy.android_douban.application.MyApplication;
import com.gzy.android_douban.utils.DialogHelp;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author : guzy
 * @program: net-app
 * @description:
 * @date : 2022-03-24 13:06
 **/
public class BaseActivity<T extends ViewBinding> extends AppCompatActivity implements DialogControl {
  protected T binding;
  private MyApplication application;
  public boolean isVisible;
  private ProgressDialog waitDialog;
  public final String TAG = this.getClass().getSimpleName();

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (application == null) {
      // 得到Application对象
      application = (MyApplication) getApplication();
    }
    addActivity();// 调用添加方法

    Type superclass = getClass().getGenericSuperclass();
    Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
    try {
      Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
      binding = (T) method.invoke(null, getLayoutInflater());
      setContentView(binding.getRoot());
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
      e.printStackTrace();
    }
    isVisible = true;
  }



  // 添加Activity方法
  public void addActivity() {
    application.addActivity_(this);// 调用myApplication的添加Activity方法
  }
  //销毁当个Activity方法
  public void removeActivity() {
    application.removeActivity_(this);// 调用myApplication的销毁单个Activity方法
  }
  //销毁所有Activity方法
  public void removeALLActivity() {
    application.removeALLActivity_();// 调用myApplication的销毁所有Activity方法
  }

  @Override
  public ProgressDialog showWaitDialog() {
    waitDialog.setCancelable(true);
    return showWaitDialog(R.string.loading);
  }

  @Override
  public ProgressDialog showWaitDialog(int resid) {
    waitDialog.setCancelable(true);
    return showWaitDialog(getString(resid));
  }

  @Override
  public ProgressDialog showWaitDialog(String message) {
    if (isVisible) {
      if (waitDialog == null) {
        waitDialog = DialogHelp.getWaitDialog(this, message);
      }
      if (waitDialog != null) {
        waitDialog.setMessage(message);
        waitDialog.setCancelable(false);
        waitDialog.show();
      }
      return waitDialog;
    }
    return null;
  }

  @Override
  public void hideWaitDialog() {
    if (isVisible && waitDialog != null) {
      try {
        Log.d(TAG,"hideWaitDialog");

        waitDialog.setCancelable(true);
        waitDialog.dismiss();
        waitDialog = null;
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }


}